Debug = Debug or {}
DebugInfo = {}

local function toString(context)
    local result = ""
    if(Debug:GetType(context) == "table") then
        result = "{"
        for i,v in pairs(context) do
            result = result..(type(i)=="number"and""or tostring(i)..":")..toString(v)..", "
        end
        result = result.."}"

    elseif(Debug:GetType(context)=="Vector") then
        result = string.format("{x:%f, y:%f, z:%f}",context.x,context.y,context.z)
    elseif(Debug:GetType(context)=="Rotator") then
        result = string.format("{Pitch:%f, Yaw:%f, Roll:%f}",context.Pitch,context.Yaw,context.Roll)
    else
        result = tostring(context)
    end
    return result
end

function Debug:GetType(v)
    local result = type(v)
    if(result == "userdata") then
        result = string.sub(tostring(v),11,-20)
    end
    return result
end

function Debug:Log(...)
    local context = { ... }
    local result = "Log: "
    for i, v in ipairs(context) do
        result = result .. "   " .. toString(v)
    end
    if(DebugInfo[result] == nil) then
        DebugInfo[result] = {0,"",0}
    end
    DebugInfo[result][2] = os.date("%H %M") .. "  "..result
    DebugInfo[result][3] = DebugInfo[result][3] + 1
    print(result)
end

function Debug:Error(...)

    local context = { ... }
    local result = "Error: "
    for i, v in ipairs(context) do
        result = result .. "   " .. toString(v)
    end
    if(DebugInfo[result] == nil) then
        DebugInfo[result] = {1,"",0}
    end
    DebugInfo[result][2] = os.date("%H %M") .. "  "..result
    DebugInfo[result][3] = DebugInfo[result][3] + 1
    print(result)
end

function Debug:Clear()
    DebugInfo = {}

end

function Debug:Show(t)
    local result = ""
    for i, v in pairs(DebugInfo) do
        if (v[1] == t or t == nil)
        then
            result = result .. "\n" .. v[2] .. "          repeat "..v[3].." times"
        end
    end
    return result
end

return Debug
